﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSS_XNA.Engine.HUD.Controls;
using Microsoft.Xna.Framework.Graphics;
using DSS_XNA.Engine.Error;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using DSS_XNA.Engine.Graphics;
using DSS_XNA.Engine.World;
using Microsoft.Xna.Framework.Input;

namespace DSS_XNA.Engine.HUD
{
    public class HUD : Sprite
    {
        #region Properties

        public List<IHUDControl> Controls
        {
            get { return _controls; }
            set { _controls = value; }
        }
        private List<IHUDControl> _controls = new List<IHUDControl>();

        #endregion

        #region Public Methods

        public HUD()
        {
        }

        public void Load(ContentManager content, int width, int height, int x, int y, string backgroundName)
        {
            if (content == null)
                throw new DSSException("'content' is null.");

            this.WidthInPixels = width;
            this.HeightInPixels = height;
            this.Position = new Vector2(x, y);
            this.Texture = content.Load<Texture2D>(backgroundName);
        }

        public int Update(IWorld world, GameTime gameTime, KeyboardState ks, MouseState ms, GamePadState gs)
        {
            if (gameTime == null)
                throw new DSSException("'gameTime' is null.");

            foreach (IHUDControl control in this.Controls)
            {
                control.Run(gameTime, world.Engine);
            }

            return 1;
        }

        public void Render(SpriteBatch spriteBatch, SpriteFont font)
        {
            if (spriteBatch == null)
                throw new DSSException("'spriteBatch' is null.");

            Vector2 pos = this.Position;
            this.Render(spriteBatch);

            pos += new Vector2(5, 5);
            float x = pos.X; // just in case a control changes this, we can reset it

            foreach (IHUDControl control in this.Controls)
            {
                control.Render(spriteBatch, ref pos, font);

                pos.X = x;
            }
        }

        #endregion
    }
}
